package com.easy.common.check;

import com.baidu.aip.contentcensor.AipContentCensor;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SVFilter {

    //设置APPID/AK/SK
    @Value("${sv.APP_ID}")
    private String APP_ID;

    @Value("${sv.API_KEY}")
    private String API_KEY;

    @Value("${sv.SECRET_KEY}")
    private String SECRET_KEY;

    public String sv(String msg) {
        System.out.println(APP_ID + "," + API_KEY + "," + SECRET_KEY);
        // 初始化一个AipContentCensor
        AipContentCensor client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", 2);  // 设置http代理
//        client.setSocketProxy("proxy_host", 1);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        JSONObject res = client.textCensorUserDefined(msg);
//        System.out.println(res.toString(2));
        if (res.getString("conclusion").contains("不合规")) {
            JSONArray data = res.getJSONArray("data");
            return data.getJSONObject(0).get("msg").toString();
        } else {
            return "合规";
        }
    }

    public static void main(String[] args) {
        SVFilter svFilter = new SVFilter();
        String s = svFilter.sv("dassa你ma的");
        System.out.println(s);
    }
}
